package com.atguigu.srb.core.controller.api;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.result.Result;
import com.atguigu.common.util.RegexValidateUtils;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.VO.LoginVO;
import com.atguigu.srb.core.pojo.VO.RegisterVO;
import com.atguigu.srb.core.pojo.VO.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author ZLHao
 * @since 2021-07-12
 */
@Api(tags = "会员注册")
@RestController
@Slf4j
@RequestMapping("/api/core/userInfo")

public class UserInfoController {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVO registerVO){
        System.out.println("进来了userinfo Controller....");
        String mobile = registerVO.getMobile();
        System.out.println(mobile);
        String password = registerVO.getPassword();
        String code = registerVO.getCode();

        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //MOBILE_ERROR(-203, "手机号不正确"),
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        //CODE_NULL_ERROR(-205, "验证码不能为空"),
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);

        //校验验证码
     //   String codeGen = (String)redisTemplate.opsForValue().get("srb:sms:code:" + mobile);
        //CODE_ERROR(-206, "验证码不正确"),
        Assert.equals(code, code, ResponseEnum.CODE_ERROR);

        //注册
        Integer  ii = 33;
        Integer  ee = 33;

        boolean equals = ii.equals(ee);

        userInfoService.register(registerVO);
        return Result.ok().message("注册成功");
    }


    /**
     * @description: 用户登录
     * @param:
     * @return: com.atguigu.common.result.Result
     * @author Administrator
     * @date: 2021/7/18 0018 22:32
     */
    @ApiOperation(value = "会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO, HttpServletRequest request){
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);

        //        获取登录ip
        String ip = request.getRemoteAddr();
        UserInfoVO userInfoVO = userInfoService.login(loginVO, ip);

        return Result.ok().data("userInfo", userInfoVO);


    }


    //  用户登录成功后，下次请求携带tonken，此方法用于校验tonken
    @ApiOperation("检查Tonken")
    @GetMapping("/checkToken")
    public Result checkTonken(HttpServletRequest request){

        // 从消息头得到tonken
        String token = request.getHeader("token");

        // 检查
        boolean b = JwtUtils.checkToken(token);

        if (b){
            return Result.ok();
        }else {
            return Result.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }


    /**
     * @description: 检查手机号是否已经注册
     * @param:
     * @return: com.atguigu.common.result.Result
     * @author Administrator
     * @date: 2021/7/19 0019 17:23
     */
    @ApiOperation("检查手机号是否存在")
    @GetMapping("/checkMobile/{mobile}")
    public Boolean checkphone(
            @ApiParam("手机号码")
            @PathVariable("mobile") String mobile) throws InterruptedException {
//        Thread.sleep(2000);
        return userInfoService.checkMobile(mobile);

    }

}

